/*
 * Classe contendo todos os métodos de cálculo para estatística
 * descritiva
 *
 * @author JR Steiner
 */

package sadoee.formulas.estatistica.descritiva;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public abstract class FormulasEstatisticaDescritiva{

    //Será substituído pelo caminho para o BD.
    //path to the main data file
    private String mainPath;

    // Read the data

    public void ReadData(String path){
        setMainPath(mainPath);

        ArrayList<Double> data = new ArrayList<Double>();
        double aux= 0.0;

        try{
            FileReader reader = new FileReader(path);
            BufferedReader in = new BufferedReader(reader);
            while(in.ready()){
                in.readLine();
                data.add(aux);
            }
            in.close();
            reader.close();
        }catch(Exception e){

        }

    }

    public double Sum(File dataIn){

        double sum = 0.0;
        // Load Data

        try {

            File fileIn = dataIn;

            if(fileIn.isFile() == true){

                FileReader reader = new FileReader(fileIn);
                BufferedReader in = new BufferedReader(reader);

                double data[] = new double[(int) fileIn.length()];

                for (int i = 0 ; i < (int) fileIn.length() ; i++){
                    data[i] = System.in.read();
                }

                in.close();
                reader.close();

                // evaluate the sum of all data

                for(int i = 0; i < data.length ; i++){
                    sum +=data[i];
                }
            }
        }catch(Exception e){

        }
        return sum;
    }

    public double CalculaMedia(File dataIn){

        // sum over all data
        double mean = 0.0;
                // evaluate the mean
                mean = Sum(dataIn) / (dataIn.length());


        // Save Data
        try {
            File fileOut = new File("./mean.dat");
            FileWriter writer = new FileWriter(fileOut);
            PrintWriter saida = new PrintWriter(writer);
            saida.printf("%6.4f",mean);
            saida.close();
            writer.close();
        } catch (Exception e) {

        }
        return mean;
    }

    public void CalculaMomentum(File dataIn, int N){

        double moment[] = new double[N];

        for( int i = 1 ; i < N ; i++){
            moment[i] = Math.pow(Sum(dataIn), N);
        }

        // Save Data
        try {
            File fileOut = new File("./moment.dat");
            if(fileOut.isFile() == true){
                FileWriter writer = new FileWriter(fileOut);
                PrintWriter saida = new PrintWriter(writer);

                for( int i = 1 ; i < N ; i++){
                    saida.printf("%6.4f\t",moment[i]);
                }
                saida.close();
                writer.close();
            }else{
                System.err.println("Entre com um nome de arquivo válido.");
            }
        } catch (Exception e) {
        }
    }


    public double CalculaDesvioPadrao(File dataIn){

        int N = (int)dataIn.length();
        double stdDeviation = 0.0;
        double data[] = new double[N];
        data[0] = 0.0;
        double dif = 0.0;
        double m = CalculaMedia(dataIn);


            if(dataIn.isFile() == true){
            try{
                FileReader reader = new FileReader(dataIn);
                BufferedReader in = new BufferedReader(reader);

                for (int i = 0 ; i < (int) dataIn.length() ; i++){
                    data[i] = System.in.read();
                }
                in.close();
                reader.close();
                }catch(IOException e){
                }

            for(int i = 0 ; i < N ; i++){
                dif += data[i] - m;
            }

            double factor = 0.0;
            factor = Math.pow(dif,2)/(N-1);
            stdDeviation = Math.sqrt(factor);

        }else{
            System.err.println("Erro de arquivo");
        }
        return stdDeviation;
    }


    public void CalculaADistribuicaoGaussiana(File dataIn){

        int i = 0;
        int N = (int)dataIn.length();
        double data[] = new double[N];
        double normalDistribution[] = new double[N];

        normalDistribution[0] = 0.0;
        data[0] = 0.0;
        double dif = 0.0;
        double std = CalculaDesvioPadrao(dataIn);
        double m = CalculaMedia(dataIn);

        if(dataIn.isFile() == true){
            try{
                FileReader reader = new FileReader(dataIn);
                BufferedReader in = new BufferedReader(reader);

                for (i = 0 ; i < N ; i++){
                    data[i] = System.in.read();
                }
                in.close();
                reader.close();
                }catch(IOException e){

                }

            for(i = 0 ; i < N ; i++){
                dif += data[i] - m;
            }
            double factor = 0.0;
            factor = Math.pow(dif,2);

            for(i = 0 ; i < N ; i++){
                normalDistribution[i] += Math.exp(factor/(2*std));
            }
        }
        // Save Data
        try {
            File fileOut = new File("./normalDistribution.dat");
            FileWriter writer = new FileWriter(fileOut);
            PrintWriter saida = new PrintWriter(writer);
            saida.printf("%6.4f\t%d",normalDistribution[i],i);
            saida.close();
            writer.close();
        } catch (Exception e) {

        }
    }

    /**
     * @return the mainPath
     */
    public String getMainPath() {
        return mainPath;
    }

    /**
     * @param mainPath the mainPath to set
     */
    public void setMainPath(String mainPath) {
        this.mainPath = mainPath;
    }
}